package com.donn.securitycontrolplatform.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.db1.mapper.TRoleMapper;
import com.donn.securitycontrolplatform.role.entity.TRole;
import com.donn.securitycontrolplatform.role.service.ITRoleService;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author zhenglixiang
 * @since 2022-06-10
 */
@Service
@Slf4j
public class TRoleServiceImpl extends ServiceImpl<TRoleMapper, TRole> implements ITRoleService {

    @Autowired
    TRoleMapper tRoleMapper;

    /**
     * 角色新增
     * @param role 角色
     * @return
     */
    @Override
    public Result insertRole(TRole role) {
        try {
            role.setCreateTime(LocalDateTime.now());
            return Result.success(save(role));
        } catch (Exception e) {
            log.error("insertRole error :", e);
        }
        return Result.init(500, "fail", "");
    }

    /**
     * 角色更新
     * @param role 角色
     * @return
     */
    @Override
    public Result updateRole(TRole role) {
        try {
            role.setUpdateTime(LocalDateTime.now());
            if (updateById(role)) {
                return Result.success("");
            }
        } catch (Exception e) {
            log.error("updateRole error :", e);
        }
        return Result.init(500, "fail", "");
    }

    /**
     * 角色删除
     * @param roleId 角色id
     * @return 删除结果
     */
    @Override
    public Result deleteRole(String roleId) {
        try {
            if (removeById(roleId)) {
                return Result.success("");
            }
        } catch (Exception e) {
            log.error("deleteRole error :", e);
        }
        return Result.init(500, "fail", "");
    }

    /**
     * 角色详情
     * @param roleId 角色id
     * @return 角色详情
     */
    @Override
    public Result getRoleById(String roleId) {
        try {
            return Result.success(getById(roleId));
        } catch (Exception e) {
            log.error("deleteRole error :", e);
        }
        return Result.init(500, "fail", "");
    }

    /**
     * 角色列表
     * @param role 过滤条件
     * @param page 分页参数
     * @return 角色列表
     */
    @Override
    public Result selectRoleList(TRole role, Page page) {
        try {
            QueryWrapper<TRole> queryWrappe = new QueryWrapper<>();
            if (StringUtil.isNotEmpty(role.getRoleName())) {
                queryWrappe.lambda().eq(TRole::getRoleName, role.getRoleName());
            }
            if (null != role.getStatus()) {
                queryWrappe.lambda().eq(TRole::getStatus, role.getStatus());
            }
            if (StringUtil.isNotEmpty(role.getCreateBy())) {
                queryWrappe.lambda().eq(TRole::getCreateBy, role.getCreateBy());
            }
            return Result.success(page(page, queryWrappe));
        } catch (Exception e) {
            log.error("selectRole error :", e);
        }
        return Result.init(500, "fail", "");
    }

    /**
     * 通过用户id查询角色
     * @param userId 用户id
     * @return 角色详情
     */
    @Override
    public TRole getRoleByUserId(Long userId) {
        return getById(userId);
    }
}
